﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinLotto
{
    static class ArrayExtensions
    {
        static Random random = new Random();
        
        public static T[] Shuffle<T>(this T[] self, int count = 0)
        {
            if (count > self.Length)
                throw new ArgumentException("count must be less than or equal to the length of the array", "count");

            if (count == 0)
                count = self.Length;

            for (int i = 0; i < count; i++) {
                var k = random.Next(i, self.Length);
                self.Swap(i, k);
            }

            return self;
        }

        public static void Swap<T>(this T[] self, int i, int j)
        {
            T temp = self[i];
            self[i] = self[j];
            self[j] = temp;
        }

        public static string DelimitedString<T>(this T[] self, string delimiter, int index = 0, int count = 0)
        {
            if (index + count > self.Length)
                throw new ArgumentException("index + count must be less than or equal to the length of the array");

            if (count == 0)
                count = self.Length - index;

            var sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                if (sb.Length > 0)
                    sb.Append(delimiter);
                sb.Append(self[index + i].ToString());
            }
            return sb.ToString();
        }
    }
}
